package com.kly.user.mapper;

import cn.hutool.core.date.DateUtil;
import com.fqgj.common.utils.TimeUtils;
import com.kly.user.db.entity.UserChatterMappingEntity;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.dto.UserBaseDto;
import com.kly.user.dto.UserChangeDto;
import com.kly.user.dto.UserCodeDto;
import com.kly.user.dto.UserInfoDto;
import com.kly.user.dto.chatter.response.UserChatterModeDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.Date;
import java.util.List;

@Mapper(componentModel = "spring")
public interface UserMapper {

    UserEntity UserInfoDtoToUser(UserInfoDto userLogin);

    @Mapping(source = "birthday", target = "age", qualifiedByName = "getAge")
    @Mapping(source = "status", target = "userValid", qualifiedByName = "isUserValid")
    UserInfoDto UserDtoToUserInfo(UserEntity user);

    UserCodeDto toUserCodeDto(UserEntity user);

    @Mapping(target = "userCode", constant = "null")
    @Mapping(source = "images", target = "avatarUrl", qualifiedByName = "firstElement")
    UserEntity UserChangeToUser(UserChangeDto UserChange);

    @Mapping(source = "status", target = "isUserValid", qualifiedByName = "isUserValid")
    UserBaseDto UserToUserBase(UserEntity user);

    @Named("isUserValid")
    default Boolean isUserValid(Integer status) {
        return status==2;
    }


    @Named("getAge")
    default Integer getAge(Date date) {
        if (date==null){
            return 0;
        }else {
            return TimeUtils.getAge(date);
        }
    }

    @Named("firstElement")
    default String firstElement(List<String> list) {
        return (list != null && !list.isEmpty()) ? list.get(0) : null;
    }


    UserChatterModeDto chatModeToDto(UserChatterMappingEntity entity);

}
